Linux 下c获取当前时间(精确到秒和毫秒或者微秒) 您所在的位置:网站首页 linux usb声卡 10毫秒 Linux 下c获取当前时间(精确到秒和毫秒或者微秒)

Linux 下c获取当前时间(精确到秒和毫秒或者微秒)

2023-06-30 18:51| 来源: 网络整理| 查看: 265

转载: https://blog.csdn.net/deyuzhi/article/details/51814934 https://blog.csdn.net/jinchengzhou/article/details/72550802

获取当前的时间的秒数和微秒数本方法需要用到gettimeofday()函数,该函数需要引入的头文件是 sys/time.h 。

函数说明int gettimeofday (struct timeval * tv, struct timezone * tz)

1、返回值:该函数成功时返回0,失败时返回-1 2、参数

struct timeval{ long tv_sec; //秒 long tv_usec; //微秒 }; struct timezone { int tz_minuteswest; //和Greenwich 时间差了多少分钟 int tz_dsttime; //日光节约时间的状态 };

3、示例

#include #include #include #include long timestamp() { struct timeval tv; gettimeofday(&tv, NULL); return (tv.tv_sec*1000 + tv.tv_usec/1000); } #include #include #include #include #include int main(){ struct timeval tv; gettimeofday(&tv,NULL); printf("second:%ld\n",tv.tv_sec); //秒 printf("millisecond:%ld\n",tv.tv_sec*1000 + tv.tv_usec/1000); //毫秒 printf("microsecond:%ld\n",tv.tv_sec*1000000 + tv.tv_usec); //微秒 sleep(3); // 为方便观看,让程序睡三秒后对比 std::cout public staticvoid main(String[]args){ try { long startTime =System.currentTimeMillis(); Thread.sleep(3000); long endTime =System.currentTimeMillis(); System.out.println("time:" + (endTime -startTime) +" ms"); } catch(InterruptedExceptione){ e.printStackTrace(); } } }

通过Google找了一些资料后,发现C语言里没有标准的接口可以获得精确到毫秒的时间,都会调用到与操作系统相关的API,下面会分别介绍在Linux和Windows系统下的多种实现方法,希望对大家有帮助。 Linux系统

使用gettimeofday接口: 下载: gettimeofday.c

#include #include int main(){ struct timevalstart, end; gettimeofday( &start,NULL ); sleep(3); gettimeofday( &end,NULL ); int timeuse =1000000 * ( end.tv_sec -start.tv_sec) + end.tv_usec -start.tv_usec; printf("time: %d us\n",timeuse); return 0; }

gettimeofday能得到微秒数,比毫秒还要更精确。

使用ftime接口:

#include #include long longgetSystemTime(){ struct timebt; ftime(&t); return 1000 *t.time +t.millitm; } int main(){ long longstart=getSystemTime(); sleep(3); long longend=getSystemTime(); printf("time: %lld ms\n",end-start); return 0; }

Windows系统

使用GetTickCount接口:

#include #include int main(){ DWORD start,stop; start =GetTickCount(); Sleep(3000); stop =GetTickCount(); printf("time: %lld ms\n",stop - start); return 0; }

Windows系统下有些编译器使用printf输出64位整数参数要使用%I64d,比如VC。

使用QueryPerformanceX接口:

#include #include int main(){ LARGE_INTEGER li; LONGLONG start,end, freq; QueryPerformanceFrequency(&li); freq =li.QuadPart; QueryPerformanceCounter(&li); start =li.QuadPart; Sleep(3000); QueryPerformanceCounter(&li); end =li.QuadPart; int useTime =(int)((end - start) *1000 / freq); printf("time: %d ms\n",useTime); return 0; }

使用GetSystemTime接口: 下载: GetSystemTime.c

#include #include int main(){ SYSTEMTIME currentTime; GetSystemTime(¤tTime); printf("time: %u/%u/%u %u:%u:%u:%u %d\n", currentTime.wYear,currentTime.wMonth,currentTime.wDay, currentTime.wHour,currentTime.wMinute,currentTime.wSecond, currentTime.wMilliseconds,currentTime.wDayOfWeek); return 0; }

这种方法没给出计算时间差的实现,只给出如何用GetSystemTime调用得到当前时间,计算时间差比较简单,根据年、月、日、时、分秒和毫秒计算出一个整数,再将两整数相减即可。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有